package lottery

import (
	"context"
	"sale-admin/config/mysql"
	"sale-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/helper"
	"github.com/golang-module/carbon"
)

type LotteryOrderService struct{}

// CheckOrderOfLottery 抽奖前的订单查询
func (ls LotteryOrderService) CheckOrder(
	ctx context.Context,
	OutTradeNo string,
) (
	orderCount int64,
	err error,
) {
	db := mysql.NewDB()
	tx := db.Model(&models.MaOrder{})
	tx.Where("end_out_trade_no = ? || out_trade_no = ?", OutTradeNo, OutTradeNo)
	tx.Count(&orderCount)
	if orderCount == 0 {
		return
	}

	return
}

// [new] 抽奖订单查询，防止重复抽奖
func (ls LotteryOrderService) CheckOrderOfLottery(
	ctx context.Context,
	ActivityID string,
	OutTradeNo string,
) (
	count int64,
	err error,
) {
	db := mysql.NewDB()
	tx := db.Model(&models.MaLotteryOrder{})
	if OutTradeNo != "" && ActivityID != "" {
		tx.Where("old_out_trade_no = ? and activity_id = ?", OutTradeNo, ActivityID)
	}
	tx.Count(&count)
	return
}

// 包含会员抽奖、中奖信息
type MyMemberInfoLog1 struct {
	models.MaLotteryMemberActiveLog
	// 订单金额
	PaymentAmount float64 `gorm:"column:payment_amount;type:decimal(10,2)" json:"payment_amount"`
	// 订单时间
	AddTime string `gorm:"column:add_time;type:string" json:"add_time"`
}

// Detail 日志详情
func (ls LotteryOrderService) LogList(
	ctx context.Context,
	Page, Limit int,
	Phone string,
	MemberID string,
) (
	list []MyMemberInfoLog1,
	count int64,
	err error,
) {
	// 定义一个空数组
	list = []MyMemberInfoLog1{}
	db := mysql.NewDB()
	tx := db.Table(models.MaLotteryMemberActiveLog{}.TableName() + " as l").
		Joins("LEFT JOIN " + models.MaOrder{}.TableName() + " as o ON l.out_trade_no = o.out_trade_no")
	tx.Where("l.customer_id = ?", MemberID)
	// 统计总条数
	tx.Group("l.id")
	tx.Count(&count)
	// 获取日志数据,并且关联订单信息
	helper.Gorm{}.
		Paginate(tx, int(Page), int(Limit)).
		Select("o.payment_amount, o.add_time, l.*").
		Find(&list)
	return
}

// List 日志列表
func (ls LotteryOrderService) List(
	ctx context.Context,
	merchantID string,
	Page, Limit int,
	Phone string,
) (
	list []models.MaLotteryMemberActiveLog,
	// list []MyMemberInfo,
	count int64,
	err error,
) {
	db := mysql.NewDB()
	tx := db.Model(&models.MaLotteryMemberActiveLog{})
	// 有哪些检索的字段？
	tx.Count(&count)
	// 赋空值
	list = []models.MaLotteryMemberActiveLog{}
	helper.Gorm{}.Paginate(tx, int(Page), int(Limit)).Find(&list)
	return
}

// Add 添加日志
func (ls LotteryOrderService) Add(
	Type int,
	SalesID,
	ActivityID,
	MemberID,
	OutTradeNo,
	Phone string,
	LotteryCode string,
) (err error) {

	db := mysql.NewDB()
	// 生成日志ID
	UUID, _ := helper.UUID{}.String()
	var data models.MaLotteryMemberActiveLog
	data.ID = UUID
	data.Type = Type
	data.SalesID = SalesID
	if MemberID != "" {
		data.CustomerID = MemberID
	}
	data.LotteryActivityID = ActivityID
	data.OutTradeNo = OutTradeNo
	if Phone != "" {
		data.Phone = Phone
	}
	data.CreatedDate = carbon.Now().StartOfDay().ToDateString() // 当前的添加日期
	data.LotteryCode = LotteryCode                              // type == 2 时，才有值
	// 执行写入
	err = db.Model(&models.MaLotteryMemberActiveLog{}).Create(&data).Error
	if err != nil {
		err = errorx.New("新增日志失败！", -1)
		return
	}
	return
}
